package com.sskj.xgja;

import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleObserver;
import android.arch.lifecycle.OnLifecycleEvent;

import java.lang.ref.WeakReference;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

public class BasePresenter<V extends BaseView> implements LifecycleObserver {

    public WeakReference<V> mViewRef;

    public V mView;

    private CompositeDisposable requests = new CompositeDisposable();

    public void addRequest(Disposable disposable) {
        requests.add(disposable);
    }

    public void attachView(V view) {
        mView = view;
        mViewRef = new WeakReference(mView);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    public void onDestroy() {
        mView = null;
        if (mViewRef != null) {
            mViewRef.clear();
        }
        requests.dispose();
    }

}


